﻿<?php
require ("webfunc.php");
$globalsid = '';
function getmintime($data){
	preg_match_all("/还有([0-9\x{4e00}-\x{9fa5}]+分)/u", $data, $matchtime);
	$mintime = 99999999;
	for ($i=0; $i<count($matchtime[1]); $i++)
	{
		preg_match("/([0-9]+)天/u", $matchtime[1][$i], $matchday);
		if (count($matchday) != 0)
		{
			$day = $matchday[1];
		}
		else
		{
			$day = '0';
		}
		preg_match("/([0-9]+)小时/u", $matchtime[1][$i], $matchhour);
		if (count($matchhour) != 0)
		{
			$hour = $matchhour[1];
		}
		else
		{
			$hour = '0';
		}
		preg_match("/([0-9]+)分/u", $matchtime[1][$i], $matchminute);
		if (count($matchminute) != 0)
		{
			$minute = $matchminute[1];
		}
		else
		{
			$minute = '0';
		}
		$temptime = intval($day)*1440 + intval($hour)*60 + intval($minute) + 1;
		if($temptime < $mintime)
		{
			$mintime = $temptime;
		}
		//echo($matchtime[1][$i]."|day:".$day."|hour:".$hour."|minute:".$minute."<br />");
		//echo($temptime."<br />");
	}
	return $mintime;
}

$origURL= '/home.do';
if(array_key_exists('email',$_GET))
{
	$useremail = $_GET['email'];
	if($useremail == 'flamepjlh@126.com')
		$globalsid = $global_126;
	if($useremail == 'flamepjlh@163.com')
		$globalsid = $global_163;
	if($useremail == 'fxckgfw1@gmail.com')
		$globalsid = $global_gmail;
}
else
{
	$useremail = 'flamepjlh@126.com';
}
if(array_key_exists('pwd',$_GET))
{
	$userpwd = $_GET['pwd'];
}
else
{
	$userpwd = 'flamehjhj';
}
$login = '登录';
$curlPost = 'origURL='  . urlencode($origURL) . '&email=' . urlencode($useremail) . '&password='. urlencode($userpwd). '&login='. urlencode($login);
//$data = vlogin('http://3g.renren.com/login.do?fx=0&autoLogin=true', $curlPost);
$loginurl = 'http://3g.renren.com/home.do?sid='.$globalsid;
$data = vget($loginurl);
$ret1 = strip_tags($data, "<a>");
$ret2 = substr($ret1, strpos($ret1, 'sid='));
$sid = substr($ret2, strpos($ret2, 'sid='),strpos($ret2, '&amp') - strpos($ret2, 'sid='));
$ret1 = substr($ret1, strpos($ret1, 'http://mapp.renren.com/apps.do?'));
$appurl = substr($ret1, strpos($ret1, 'http://mapp.renren.com/apps.do?'), strpos($ret1, '>应用<') - strpos($ret1, 'http')-1);
$data = vget('http://mapps.renren.com/rr_farm/farm/action/wap,indexAction.php?'.$sid);
//$data = vget($appurl);
//$ret1 = substr($data, strpos($data, 'http://mapp.renren.com/apphistory.do?appid=53429'));
//$appurl = substr($ret1, strpos($ret1, 'http'), strpos($ret1, 'htf=751') - strpos($ret1, 'http')+7);
//$data = vget($appurl);
$coloreig = '/rr_farm/farm/action/wap,indexAction.php?&svv=1';
$appmainurl = 'http://mapps.renren.com';
$cropeig = '/rr_farm/farm/action/wap,myCropAction';
$treeeig = '/rr_farm/farm/action/wap,myTreeAction';
$animaleig = '/rr_farm/farm/action/wap,myAnimalAction';
$machineeig = '/rr_farm/farm/action/wap,myMachineAction';
$friendeig = '/rr_farm/farm/action/wap,myFriendsListAction';

if (strpos($data, $coloreig))
{
	$ret1 = substr($data, strpos($data, $coloreig));
	$simpleurl = $appmainurl.substr($ret1, strpos($ret1, $coloreig), strpos($ret1, ">简版") - strpos($ret1, $coloreig) - 1);
	$data = vget($simpleurl);
}
$cropurl = $appmainurl.substr($data, strpos($data, $cropeig), strpos($data, ">【农田】") - strpos($data, $cropeig) - 1);
$treeurl = $appmainurl.substr($data, strpos($data, $treeeig), strpos($data, ">【果树】") - strpos($data, $treeeig) - 1);
$animalurl = $appmainurl.substr($data, strpos($data, $animaleig), strpos($data, ">【畜牧】") - strpos($data, $animaleig) - 1);
$machineurl = $appmainurl.substr($data, strpos($data, $machineeig), strpos($data, ">【机械】") - strpos($data, $machineeig) - 1);
$cropdata = vget($cropurl);
$mintime = getmintime($cropdata);
$page = 1;
while (strpos($cropdata, '下一页'))
{
	if(strpos($cropdata, '收获'))
	{
		$mintime = 1;
	}
	$cropurl2 = $cropurl."&curpage=".$page;
	$cropdata = vget($cropurl2);
	$temptime = getmintime($cropdata);
	if($temptime < $mintime)
	{
		$mintime = $temptime;
	}
	$page = $page + 1;
	//echo('page:'.$page."MinTime:".$mintime."<br />");
}
$treedata = vget($treeurl);
$temptime = getmintime($treedata);
if($temptime < $mintime)
{
	$mintime = $temptime;
}
$page = 1;
while(strpos($treedata, '下一页'))
{
	if(strpos($cropdata, '收获'))
	{
		$mintime = 1;
	}
	$treeurl2 = $treeurl."&curpage=".$page;
	$treedata = vget($treeurl2);
	$temptime = getmintime($treedata);
	if($temptime < $mintime)
	{
		$mintime = $temptime;
	}
	$page = $page + 1;
	//echo('page:'.$page."MinTime:".$mintime."<br />");
}
$animaldata = vget($animalurl);
$temptime = getmintime($animaldata);
if($temptime < $mintime)
{
	$mintime = $temptime;
}
$page = 1;
while(strpos($animaldata, '下一页'))
{
	$animalurl2 = $animalurl."&curpage=".$page;
	$animaldata = vget($animalurl2);
	$temptime = getmintime($animaldata);
	if($temptime < $mintime)
	{
		$mintime = $temptime;
	}
	$page = $page + 1;
	//echo('page:'.$page."MinTime:".$mintime."<br />");
}
$machinedata = vget($machineurl);
$temptime = getmintime($machinedata);
if($temptime < $mintime)
{
	$mintime = $temptime;
}
$page = 1;
while(strpos($machinedata, '下一页'))
{
	$machineurl2 = $machineurl."&curpage=".$page;
	$machinedata = vget($machineurl2);
	$temptime = getmintime($machinedata);
	if($temptime < $mintime)
	{
		$mintime = $temptime;
	}
	$page = $page + 1;
	//echo('page:'.$page."MinTime:".$mintime."<br />");
}
$setcronurl = "http://flamehe.appspot.com/reapall?setcron=true&time=".$mintime."&email=".$useremail."&pwd=".$userpwd;
$addresult = vget($setcronurl);
echo($mintime);
echo($setcronurl);
echo($addresult);
delcookie($cookie_file);
?>